package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.RechargeRecordBO;
import com.junzhiit.nengji.bo.RechargeRecordSearchBO;
import com.junzhiit.nengji.dao.RechargeRecordDAO;
import com.junzhiit.nengji.model.RechargeRecord;
import com.junzhiit.nengji.model.enums.RechargeRecordStatus;

@Repository
public class RechargeRecordDAOImpl extends AbstractBaseDAO<RechargeRecord> implements RechargeRecordDAO {

	@Override
	public List<RechargeRecordBO> findAll(RechargeRecordSearchBO bo, int pageNumber, int maxPageRow) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		
		sql.append(" select rr.*,if(u.name is null,u.nick_name,u.name)userName ");
		sql.append(" from recharge_record rr left join `user` u on rr.user_id = u.id where rr.is_delete = ?");
		
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(bo.getOrderNumber())){
			sql.append(" and rr.order_number = ? ");
			param.setString(bo.getOrderNumber());
		}
		
		if(StringUtils.isNotBlank(bo.getUserId())){
			sql.append(" and rr.user_id = ? ");
			param.setString(bo.getUserId());
		}
		
		if(bo.getPayWay()!=null){
			sql.append(" and rr.pay_way = ? ");
			param.setInt(bo.getPayWay());
		}
		
		if(bo.getRechargeMoney()!=null){
			sql.append(" and rr.recharge_money = ? ");
			param.setDouble(bo.getRechargeMoney());
		}
		
		if(bo.getGiveType()!=null){
			sql.append(" and rr.give_type = ? ");
			param.setInt(bo.getGiveType());
		}
		
		if(bo.getGiveValue()!=null){
			sql.append(" and rr.give_value = ? ");
			param.setDouble(bo.getGiveValue());
		}
		
		if(StringUtils.isNotBlank(bo.getUserName())){
			sql.append(" and u.name like ? ");
			param.setString("%"+bo.getUserName()+"%");
		}
		
		if(bo.getStartTime()!=null){
			sql.append(" and rr.created_time >= ? ");
			param.setDate(bo.getStartTime());
		}
		
		if(bo.getEndTime()!=null){
			sql.append(" and rr.created_time < ? ");
			param.setDate(bo.getEndTime());
		}
		
		if(bo.getStatus()!=null){
			sql.append(" and rr.status = ? ");
			param.setInt(bo.getStatus());
		}
		
		sql.append(" order by rr.created_time desc ");
		
		return super.queryForPagingList(sql.toString(), RechargeRecordBO.class, param, pageNumber,maxPageRow);
	}

	@Override
	public int getTotalCount(RechargeRecordSearchBO bo) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		
		sql.append("  select count(*) from recharge_record rr left join `user` u on rr.user_id = u.id where rr.is_delete = ?  ");
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(bo.getOrderNumber())){
			sql.append(" and rr.order_number = ? ");
			param.setString(bo.getOrderNumber());
		}
		
		if(StringUtils.isNotBlank(bo.getUserId())){
			sql.append(" and rr.user_id = ? ");
			param.setString(bo.getUserId());
		}
		
		if(bo.getPayWay()!=null){
			sql.append(" and rr.pay_way = ? ");
			param.setInt(bo.getPayWay());
		}
		
		if(bo.getRechargeMoney()!=null){
			sql.append(" and rr.recharge_money = ? ");
			param.setDouble(bo.getRechargeMoney());
		}
		
		if(bo.getGiveType()!=null){
			sql.append(" and rr.give_type = ? ");
			param.setInt(bo.getGiveType());
		}
		
		if(bo.getGiveValue()!=null){
			sql.append(" and rr.give_value = ? ");
			param.setDouble(bo.getGiveValue());
		}
		
		if(StringUtils.isNotBlank(bo.getUserName())){
			sql.append(" and u.name like ? ");
			param.setString("%"+bo.getUserName()+"%");
		}
		
		if(bo.getStartTime()!=null){
			sql.append(" and rr.created_time >= ? ");
			param.setDate(bo.getStartTime());
		}
		
		if(bo.getEndTime()!=null){
			sql.append(" and rr.created_time < ? ");
			param.setDate(bo.getEndTime());
		}
		
		if(bo.getStatus()!=null){
			sql.append(" and rr.status = ? ");
			param.setInt(bo.getStatus());
		}
		
		return super.queryForInt(sql.toString(), param);
	}

	@Override
	public RechargeRecord findByNumber(String orderNumber) {
		String sql = "select * from recharge_record where order_number = ? and status = ? and is_delete=? ";
		StatementParameter param=new StatementParameter();
		param.setString(orderNumber);
		param.setInt(RechargeRecordStatus.UNPAID.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		return super.query(sql, RechargeRecord.class, param);
	}

	@Override
	public boolean updateToDelete(String orderNumber) {
		String sql = "update recharge_record set is_delete = ? where order_number = ? ";
		StatementParameter param=new StatementParameter();
		param.setString(orderNumber);
		param.setInt(IsDelete.YES.toIntValue());
		return super.updateForBoolean(sql, param);
	}

}
